๋ฌธ์
Programmers - ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ
๐ Logic
// ์ฐจ ๋ฒํธ๋ณ ์
์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map
static Map<String, Integer> carTimeMap = new HashMap<>();
// ์ฐจ ๋ฒํธ๋ณ ๋์ ์ฃผ์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map
static Map<String, Integer> accumCarMap = new HashMap<>();
- ์ฐจ ๋ฒํธ๋ณ ์ ์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map์ ์ฌ์ฉํด, ์ ์ฐจ ์์๋ map์ ์ถ๊ฐํ๊ณ , ์ถ์ฐจ ์์๋ ์ ์ฐจ ์๊ฐ๊ณผ ๋น๊ตํ์ฌ ์ฃผ์ฐจ ์๊ฐ์ ๊ตฌํ๋ค.
- ์ฐจ ๋ฒํธ๋ณ ๋์ ์ฃผ์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map์ ์ฌ์ฉํด ์ฐจ ๋ฒํธ๋ณ ์ฃผ์ฐจ ์๊ฐ์ ๋์ ํฉํ๋ค.
readRecords(records);
processRemainingCars();
calculateFees();
readRecords()๋ ์ฃผ์ฐจ ๊ธฐ๋ก์ ์ฝ์ผ๋ฉด์ ์ฐจ ๋ฒํธ๋ณ ์ฃผ์ฐจ ์๊ฐ์ ๊ณ์ฐํ๋ค.processRemainingCars()์์๋ ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ์ฐจ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ์ํํ๋ค.calculateFees()์์๋ ์ฐจ ๋ฒํธ๋ณ ์ฃผ์ฐจ ์๊ธ์ ๊ณ์ฐํ๊ณ ๋ฐํํ ๋ฐฐ์ด์ ๋ด๋๋ค.
๐ CODE
์ฝ๋ ๋ณด๊ธฐ/์ ๊ธฐ๐ซ
import java.util.*;
class Solution {
static int basicTime;
static int basicFee;
static int unitTime;
static int unitFee;
// ์ฐจ ๋ฒํธ๋ณ ์
์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map
static Map<String, Integer> carTimeMap = new HashMap<>();
// ์ฐจ ๋ฒํธ๋ณ ๋์ ์ฃผ์ฐจ ์๊ฐ์ ์ ์ฅํ๋ map
static Map<String, Integer> accumCarMap = new HashMap<>();
public int[] solution(int[] fees, String[] records) {
init(fees);
readRecords(records);
processRemainingCars();
return calculateFees();
}
private static void init(int[] fees) {
basicTime = fees[0];
basicFee = fees[1];
unitTime = fees[2];
unitFee = fees[3];
}
private static void readRecords(String[] records) {
for (String record : records) {
String[] info = record.split(" ");
int time = timeToInt(info[0]);
String carN = info[1];
boolean in = Objects.equals(info[2], "IN");
// ์
์ฐจํ ๊ฒฝ์ฐ
if (in) carTimeMap.put(carN, time);
// ์ถ์ฐจํ ๊ฒฝ์ฐ
else {
// ์
์ฐจ ์๊ฐ ๊ฐ์ ธ์ด
int inTime = carTimeMap.get(carN);
// ์ฃผ์ฐจ ์๊ฐ ๊ณ์ฐ
time -= inTime;
accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time);
carTimeMap.remove(carN);
}
}
}
private static int timeToInt(String info) {
String[] line = info.split(":");
int hour = Integer.parseInt(line[0]) * 60;
int min = Integer.parseInt(line[1]);
return hour + min;
}
// ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ์ฐจ ์ฒ๋ฆฌ
private static void processRemainingCars() {
for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) {
String carN = entry.getKey();
int time = 1439 - entry.getValue();
accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time);
}
}
// ์ฐจ ๋ฒํธ๋ณ ๋์ ์ฃผ์ฐจ ์๊ฐ์ ์ด์ฉํด ์ฃผ์ฐจ ์๊ธ ๊ณ์ฐ
private static int[] calculateFees() {
// ์ฐจ ๋ฒํธ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
Object[] keys = accumCarMap.keySet().toArray();
Arrays.sort(keys);
int[] answer = new int[accumCarMap.size()];
int idx = 0;
// ์ ๋ ฌ๋ ์ฐจ ๋ฒํธ ์์ผ๋ก ๋์ ์๊ธ ๊ณ์ฐ
for (Object key : keys) {
int time = accumCarMap.get(key.toString());
int fee = basicFee;
if (time > basicTime) {
time = (int) Math.ceil((double) (time - basicTime) / unitTime);
fee += (time * unitFee);
}
answer[idx] = fee;
idx++;
}
return answer;
}
}
โ๏ธ Review
- ํ๋ผ๋ ๋๋ก ํ๋ฉด ๋๋ ๋ฌธ์ ์์ผ๋, ์๊พธ 37.5์ ์ด ๋์ค๊ธธ๋
map์ ์ ๊ทผํ๋ ์ฝ๋๋ค์ ๋ค ๋ฏ์ด๋ณด๋ค๊ฐ ์ด์ด์๋ ์ค์๋ฅผ ํ ๊ฒ์ ํ์ธํ๋ค.
// ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ์ฐจ ์ฒ๋ฆฌ
private static void processRemainingCars() {
int time = 1439;
for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) {
String carN = entry.getKey();
time = time - entry.getValue();
accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time);
}
}
// ์ถ์ฐจ ๊ธฐ๋ก์ด ์๋ ์ฐจ ์ฒ๋ฆฌ
private static void processRemainingCars() {
for (Map.Entry<String, Integer> entry : carTimeMap.entrySet()) {
String carN = entry.getKey();
int time = 1439 - entry.getValue();
accumCarMap.put(carN, accumCarMap.getOrDefault(carN, 0) + time);
}
}
time๊ฐ์ 1439๋ก ๊ณ ์ ๋์ด ์์ด์ผ ํ๋๋ฐ, ๋ฐ๋ณต๋ฌธ์ ๋๋ฉด์ ์๊พธ ๊ฐ์ํ๋๋ก ๊ตฌํํด๋์์ ํ๋ ธ๋ ๊ฒ์ด๋ค...